<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>机器人发信设置</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../assets/module/admin.css?v=318"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键词：</label>
                        <div class="layui-input-inline">
                            <input name="account" class="layui-input" placeholder="关键词"/>
                        </div>
                    </div>

                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="loginRecordTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button id="addMsgBtn" class="layui-btn icon-btn" type="button">
                            <i class="layui-icon">&#xe67d;</i>添加消息
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="wxRobotSettingsInfoTab" lay-filter="wxRobotSettingsInfoTab"></table>
        </div>
    </div>
</div>

<!-- 固定底部按钮 -->
<script type="text/html" id="dialogEditDialog1">
    <form id="dialogEditForm1"   lay-filter="dialogEditForm1" class="layui-form model-form no-padding ">
        <input name="ptId" type="hidden"/>
        <div class="model-form-body" style="height: 350px;" onscroll="layui.admin.hideFixedEl();">

            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">消息主题:</label>
                <div class="layui-input-block">
                    <input name="title" placeholder="消息主题" class="layui-input"
                           lay-verify="required" lay-verType="tips" required/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">消息内容:</label>
                <div class="layui-input-block">
                    <textarea name="msg" placeholder="" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">群链接:</label>
                <div class="layui-input-block">
                    <input name="url" placeholder="http://www.baidu.com" class="layui-input"
                           lay-verify="required" lay-verType="url" required/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">执行时间</label>
                <div class="layui-input-block">
                    <input type="time" name="exeTime" id="exeTime" lay-verify="time" placeholder="HH:mm:ss"
                           autocomplete="off" class="layui-input"   >
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">周几执行</label>
                <div class="layui-input-block"  >
                    <input type="checkbox" name="mon" title="星期一" checked="">
                    <input type="checkbox" name="tues" title="星期二">
                    <input type="checkbox" name="wed" title="星期三">
                    <input type="checkbox" name="thur" title="星期四">
                    <input type="checkbox" name="fri" title="星期五">
                    <input type="checkbox" name="sat" title="星期六">
                    <input type="checkbox" name="sun" title="星期天">
                </div>
            </div>


        </div>
        <div class="layui-form-item text-right model-form-footer">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="dialogEditSubmit1" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="userTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit" data-index="{{d.id}}">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" data-index="{{d.id}}">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="robotStatus">
    <input type="checkbox" lay-filter="robotStatusCk" value="{{d.id}}" lay-skin="switch"
           lay-text="启用|禁用" {{d.status==1?'checked':''}} style="display: none;"/>
    <p style="display: none;">{{d.status==1?'启用':'禁用'}}</p>
</script>

<!-- js部分 -->
<script type="text/javascript" src="../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../assets/js/common.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate',  'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var admin = layui.admin ;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#wxRobotSettingsInfoTab',
            url: '/wxRobot/queryWxRobotSettingsInfo',
            page: true,
            tool:  "#userTbBar",
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                // {type: 'numbers'},
                {field: 'id', title: '记录id'},
                {field: 'title', title: '消息标题'},
                {field: 'msg', title: '消息内容'},
                {field: 'groupUrl', title: '群链接'},
                {field: 'sendTimeHour', title: '消息发送时间', templet: function (d) {
                        return d.sendTimeHour + ":" + d.sendTimeMinute;
                    }
                },
                {field: 'weekday', title: '周几执行'},
                {field: 'status', title: '状态', templet: '#robotStatus', sort: true, width: 100},
                {title: '操作', toolbar: '#userTbBar', align: 'center', minWidth: 100}

            ]]
        });

        /* 渲染时间选择 */
        laydate.render({
            elem: '#exeTime'
        });


        /* 表格搜索 */
        form.on('submit(loginRecordTbSearch)', function (data) {
            if (data.field.loginRecordDateSel) {
                var searchDate = data.field.loginRecordDateSel.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            } else {
                data.field.startDate = null;
                data.field.endDate = null;
            }
            data.field.loginRecordDateSel = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });


        /* 修改send msg 状态 */
        form.on('switch(robotStatusCk)', function (obj) {
            var loadIndex = layer.load(2);
            $.post('/wxRobot/updateSendMsgRobotStatus', {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.close(loadIndex);
                if (res.code === 0) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    // form.render('checkbox');
                }

            }, 'json');
        });

        /* 表格工具条点击事件 */
        table.on('tool(wxRobotSettingsInfoTab)', function (obj) {
            if (obj.event === 'edit') { // 修改
                obj.index = $(this).data('index');
                showEditModel(obj);
            } else if (obj.event === 'del') { // 删除
                obj.id = $(this).data('index');
                doDel(obj);
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(obj) {
            admin.open({
                type: 1,
                title: '修改机器人',
                fixed: true,
                offset: 'auto',
                content: $('#dialogEditDialog1').html(),
                area: ['500px', 'auto'],
                success: function (layero, dIndex) {
                    // 回显表单数据
                    $.get("/wxRobot/queryWxRobotSettingsInfo",{id: obj.id}, function (res) {
                       var data = res.data[0];
                        console.info(JSON.stringify(data));
                        $("[name='title']").val(data.title);
                        $("[name='msg']").val(data.msg);
                        $("[name='url']").val(data.groupUrl);

                    });
                    form.render();

                    // 由于layui会对 select, checkbox等组件进行美化，所以需要重新渲染
                    form.render('select', 'dialogEditForm1');
                    form.render('checkbox', 'dialogEditForm1');

                    // 表单提交事件
                    form.on('submit(userEditSubmit)', function (data) {
                        data.field.roleIds = insRoleSel.getValue('valueStr');
                        var loadIndex = layer.load(2);
                        // 修改发信机器人的数据
                        $.post('/wxRobot/queryWxRobotSettingsInfo' , data.field, function (res) {  // 实际项目这里url可以是mData?'user/update':'user/add'
                            layer.close(loadIndex);
                            if (res.code === 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });


                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('/wxRobot/delRobotSettings', {
                    id: obj.id
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }



        /* 添加消息 */
        $('#addMsgBtn').click(function () {
            admin.open({
                type: 1,
                title: '发信机器人设置',
                fixed: true,
                offset: 'auto',
                content: $('#dialogEditDialog1').html(),
                area: ['500px', 'auto'],
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    // // 时间范围选择
                    // laydate.render({
                    //     elem: '#dialogEditForm1 input[name="dateRange"]',
                    //     range: true,
                    //     trigger: 'click'
                    // });
                    // 由于layui会对 select, checkbox等组件进行美化，所以需要重新渲染
                    form.render('select', 'dialogEditForm1');
                    form.render('checkbox', 'dialogEditForm1');
                    // 表单提交事件
                    form.on('submit(dialogEditSubmit1)', function (data) {
                        var weekday = "";
                        if(data.field.mon == "on") weekday = weekday + "1,";
                        if(data.field.tues == "on") weekday = weekday + "2,";
                        if(data.field.wed == "on") weekday = weekday + "3,";
                        if(data.field.thur == "on") weekday = weekday + "4,";
                        if(data.field.fri == "on") weekday = weekday + "5,";
                        if(data.field.sat == "on") weekday = weekday + "6,";
                        if(data.field.sun == "on") weekday = weekday + "0";

                        if(weekday.endsWith(",")){
                            weekday = weekday.substr(0,weekday.length-1)
                        }


                        var param = {
                            "title":data.field.title,
                            "msg": data.field.msg,
                            "groupUrl": data.field.url,
                            "sendTimeHour": data.field.exeTime.toString().split(":")[0],
                            "sendTimeMinute": data.field.exeTime.toString().split(":")[1],
                            "weekday":  weekday
                        };

                        console.info("提交成功, param: " + JSON.stringify(param) , {icon: 1});

                        $.ajax({
                            url:"/wxRobot/addWxRobotSettingsInfo",
                            type: 'post',
                            data: param,
                            success:function (res) {

                            },
                            error:function () {
                                layer.msg("提交失败！", {icon: 6});
                            }
                        });
                        // return false;
                    });

                }
            });
        });

    });
</script>
</body>
</html>
